/**
 * 
 */
package com.thon.dao.user.impl;

import java.util.List;

import org.springframework.stereotype.Repository;

import com.thon.dao.user.FundDetailDao;
import com.thon.dao.util.AbstractDao;
import com.thon.dao.util.Page;
import com.thon.entity.project.Project;
import com.thon.entity.user.FundDetail;
import com.thon.entity.user.User;

/**
 * @author thon
 * @email thon.ju@meet-future.com
 * @date 2011-11-10 14:18:53
 * @description:
 */
@Repository
public class FundDetailDaoImpl extends AbstractDao<FundDetail> implements
		FundDetailDao {

	public FundDetailDaoImpl() {
		super(FundDetail.class);
	}

	@Override
	public Page<FundDetail> finDetailsByProject(Page<FundDetail> page,
			Integer projectId) {
		String queryString = "FROM FundDetail f WHERE f.project.id = ?";
		return findPage(page, queryString, projectId);
	}

	@Override
	public Page<FundDetail> finDetailsByUser(Page<FundDetail> page,Integer status,
			Integer userId) {
		
		String queryString;
		if(userId==0){
			if(status==-1)
			{   queryString="SELECT * FROM ym_fund_details ORDER BY created_date desc";
				return findPageBySql(page, queryString, null);}
			else
			{
				queryString="SELECT * FROM ym_fund_details WHERE action_type = ? ORDER BY created_date desc";
				return findPageBySql(page, queryString, null,status);
			}
		}
		else
		{
		if(status==-1)
		{   queryString="SELECT * FROM ym_fund_details WHERE user_id=?  ORDER BY created_date desc";
			return findPageBySql(page, queryString, null, userId);}
		else
		{
			queryString="SELECT * FROM ym_fund_details WHERE user_id=? AND action_type = ? ORDER BY created_date desc";
			return findPageBySql(page, queryString, null, userId,status);
		}
		}
			
	}

	@Override
	public Page<FundDetail> finDetailsByUser(Page<FundDetail> page,
			Integer userId, String start) {
		String queryString = "SELECT * FROM ym_fund_details WHERE user_id=? AND created_date>=?  ORDER BY created_date desc";
		return findPageBySql(page, queryString, null, userId, start);
	}

	@Override
	public FundDetail getFundDetail(String tradeNo) {
		String queryString = "FROM FundDetail WHERE tradeNo = ?";//"from ym_fund_details where trade_no = \'"+tradeNo+"\'";
		return findOne(queryString,null,tradeNo);
	}
	
	@Override
	public FundDetail getFundDetail(Integer userId) {
		String queryString = "FROM FundDetail WHERE user.id = ? AND action_type=5 ";
		return findOne(queryString,null,userId);
	}
	
	@Override
	public Page<FundDetail> findDetails(Page<FundDetail> page,int actionType) {
		String queryString="SELECT * FROM ym_fund_details WHERE action_type=?";
		return findPageBySql(page, queryString, null,actionType);
	}

	@Override
	public Integer getSumDetailByUser(Integer id) {
		String queryString="SELECT sum(f.amount) sum FROM ym_fund_details f WHERE  f.user_id=? GROUP BY f.user_id";
		Object sum=findOneBySQL(queryString, id);
		if(sum!=null){			
			return Integer.parseInt(sum.toString());
		}else{
			return 0;
		}
	}
}
